import { request, useUserStore, clearEmpty } from "jmash-core";
import qs from "qs";
import type { AxiosPromise } from "axios";
import type {
  DictLayEntryApi, DictLayEntryReq, DictLayEntryModel, DictLayEntryPage, DictLayEntryList,
  DictLayEntryKey, DictLayEntryKeyList, DictLayEntryMoveKey, DictLayEntryEnableKey, DictLayEntryCreateReq, DictLayEntryUpdateReq,
  DictLayEntryExportReq
} from "./types";

import { dictTypeApi } from "@/api/dict/os-dict-type/index";
import type { DictTypeExportReq, DictTypeImportReq } from "@/api/dict/os-dict-type/types";

class DictLayEntryApiImpl implements DictLayEntryApi {

  getKey(model: DictLayEntryModel): DictLayEntryKey {
    return {
      dictId: model.dictId,
    } as DictLayEntryKey;
  }

  //查询翻页信息层级字典实体
  findPage(query?: DictLayEntryReq): AxiosPromise<DictLayEntryPage> {
    const tenant = useUserStore().user.tenant;
    if (query) {
      query.hasEnable = (query.enable != undefined);
    }
    clearEmpty(query);
    return request({
      url: "/v1/dict/dict_lay_entry/list/" + tenant,
      method: "get",
      params: query,
    });
  }

  //查询列表信息层级字典实体
  findList(query?: DictLayEntryReq): AxiosPromise<DictLayEntryList> {
    const tenant = useUserStore().user.tenant;
    if (query) {
      query.hasEnable = (query.enable != undefined);
    }
    clearEmpty(query);
    return request({
      url: "/v1/dict/dict_lay_entry/list/" + tenant,
      method: "get",
      params: query,
    });
  }

  //查询层级字典实体
  findById(query: DictLayEntryKey): AxiosPromise<DictLayEntryModel> {
    const tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/dict/dict_lay_entry/id/" + tenant + "/" + query.dictId,
      method: "get",
    });
  }

  // 创建实体层级字典实体
  create(data: DictLayEntryCreateReq): AxiosPromise<DictLayEntryModel> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/dict/dict_lay_entry",
      method: "post",
      data: data,
    });
  }

  //修改实体层级字典实体
  update(data: DictLayEntryUpdateReq): AxiosPromise<DictLayEntryModel> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/dict/dict_lay_entry",
      method: "patch",
      data: data,
    });
  }

  //上移下移
  move(key: DictLayEntryKey, up: boolean): AxiosPromise<boolean> {
    let data: DictLayEntryMoveKey = { ...key };
    data.tenant = useUserStore().user.tenant;
    data.up = up;
    clearEmpty(data);
    return request({
      url: "/v1/dict/dict_lay_entry/move",
      method: "put",
      data: data,
    });
  }

  //启用/禁用
  enable(key: DictLayEntryKey, enable: boolean): AxiosPromise<boolean> {
    let data: DictLayEntryEnableKey = { ...key };
    data.tenant = useUserStore().user.tenant;
    data.enable = enable;
    clearEmpty(data);
    return request({
      url: "/v1/dict/dict_lay_entry/enable",
      method: "put",
      data: data,
    });
  }

  // 删除层级字典实体
  delete(query: DictLayEntryKey): AxiosPromise<DictLayEntryModel> {
    query.tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/dict/dict_lay_entry/id",
      method: "delete",
      params: query,
    });
  }

  // 批量删除层级字典实体
  batchDelete(keys: DictLayEntryKey[]): AxiosPromise<number> {
    const ids = keys.map((item) => item.dictId);
    const data = {
      tenant: useUserStore().user.tenant,
      dictId: ids,
    } as DictLayEntryKeyList;
    clearEmpty(data);
    return request({
      url: "/v1/dict/dict_lay_entry/batch",
      method: "put",
      data: data,
    });
  }

  //下载导入模板层级字典实体
  downloadTemplate(): AxiosPromise {
    // 检查变量是否定义
    if (typeof dictTypeApi.downloadTemplate === "undefined") {
      return Promise.reject(" dictTypeApi.downloadTemplate is not undefined.");
    }
    return dictTypeApi.downloadTemplate();
  }

  // 导入层级字典实体
  importExcel(data: DictTypeImportReq): AxiosPromise<string> {
    // 检查变量是否定义
    if (typeof dictTypeApi.importExcel === "undefined") {
      return Promise.reject(" dictTypeApi.importExcel is not undefined.");
    }
    return dictTypeApi.importExcel(data);
  }

  //导出层级字典实体Excel文件
  exportExcel(queryParams: DictLayEntryExportReq): AxiosPromise {
    const req = {} as DictTypeExportReq;
    req.req = { typeCode: queryParams.req.typeCode };
    // 检查变量是否定义
    if (typeof dictTypeApi.exportExcel === "undefined") {
      return Promise.reject(" dictTypeApi.exportExcel is not undefined.");
    }
    return dictTypeApi.exportExcel(req);
  }

}

const dictLayEntryApi = new DictLayEntryApiImpl();

export { dictLayEntryApi };


